﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq.Expressions;
using System.Text;
using System.Xml;
using Wetland.Models;

namespace Wetland
{
    public class MassiveContext<T> : IDisposable
    {
        private string SYS_Wetland_DB = string.Empty;
        private string SYS_Wetland_Rel = string.Empty;

        private IDBContext DbContext;


        public MassiveContext(DBType type = DBType.SqlServer)
        {
            SYS_Wetland_DB = ConfigurationManager.AppSettings["SYS_Wetland_DB"].ToString();
            SYS_Wetland_Rel = ConfigurationManager.AppSettings["SYS_Wetland_Rel"].ToString();
            switch (type)
            {
                case DBType.SqlServer:
                    DbContext = new SqlServer.DBContext(SYS_Wetland_DB, SYS_Wetland_Rel); break;
                default:
                    DbContext = new SqlServer.DBContext(SYS_Wetland_DB, SYS_Wetland_Rel); break;
            }
        }

        public MassiveContext(string writerConn, string readerConn, DBType type = DBType.SqlServer)
        {
            SYS_Wetland_DB = ConfigurationManager.AppSettings["SYS_Wetland_DB"].ToString();
            SYS_Wetland_Rel = ConfigurationManager.AppSettings["SYS_Wetland_Rel"].ToString();
            switch (type)
            {
                case DBType.SqlServer:
                    DbContext = new SqlServer.DBContext(SYS_Wetland_DB, SYS_Wetland_Rel, writerConn, readerConn); break;
                default:
                    DbContext = new SqlServer.DBContext(SYS_Wetland_DB, SYS_Wetland_Rel, writerConn, readerConn); break;
            }
        }

        public MassiveContext(string writerConn, string readerConn, string sys_wetland_db, string sys_wetland_rel, DBType type = DBType.SqlServer)
        {
            SYS_Wetland_DB = sys_wetland_db;
            SYS_Wetland_Rel = sys_wetland_rel;
            switch (type)
            {
                case DBType.SqlServer:
                    DbContext = new SqlServer.DBContext(SYS_Wetland_DB, SYS_Wetland_Rel, writerConn, readerConn); break;
                default:
                    DbContext = new SqlServer.DBContext(SYS_Wetland_DB, SYS_Wetland_Rel, writerConn, readerConn); break;
            }
        }

        public void Add(T mdoel, DateTime? createTime)
        {
            DbContext.Add<T>(mdoel,createTime);
        }

        public List<T> Get(Expression<Func<T, bool>> where, DBPageBase page)
        {
            return DbContext.Get<T>(where, page);
        }

        public bool Add(List<T> models)
        {
            return DbContext.Add(models);
        }


        public bool Update(T entity, Expression<System.Func<T, bool>> where, List<string> ignoreColumNames, DateTime? createTime)
        {
            return DbContext.Update<T>(entity, where, ignoreColumNames, createTime);
        }

        public bool Delete(Expression<System.Func<T, bool>> where, DateTime? createTime)
        {
            return DbContext.Delete<T>(where, createTime);
        }

        public T Get(Expression<System.Func<T, bool>> where, DateTime? createTime)
        {
            return DbContext.Get<T>(where, createTime);
        }

        public void Dispose()
        {

        }
    }
}
